﻿using System.Collections;
using UnityEngine;
using System.Collections.Generic;

namespace UGUIMan
{
    public class Enemy : MonoBehaviour
    {
        public void Start()
        {
            if(Random.Range(0, 10) < 9)
            {
                Destroy(gameObject);
            }
            transform.SetParent(GameObject.Find("Hit").transform,false);
        }
        public int hit=1;
        public enum State
        {
            Null,Enter,Stay,Exit
        }
        
        public State state;
        private void OnCollisionEnter2D( Collision2D collision )
        {
            if (collision.gameObject.tag == "map"/* && !list.Exists(g => collision.gameObject == g)*/)
            {
                GetComponent<ConstantForce2D>().enabled = true;
//                GetComponent<Rigidbody2D>().gravityScale = 0;
//                transform.Rotate(new Vector3(0, 0,collision.gameObject.transform.rotation.eulerAngles.z));
//                list.Add(collision.gameObject);
            }
        }
        private void OnTriggerEnter2D( Collider2D collision )
        {
            if (collision.gameObject.tag == "Ammo")
            {
                Debug.Log("hit");
                collision.gameObject.GetComponent<SuperPower>().Kill();
                hit--;
                if (hit < 1)
                {
                    Kill();
                }
            }
        }
        private void OnCollisionExit2D( Collision2D collision )
        {
//            if (collision.gameObject.tag == "map"&& list.Exists(g => collision.gameObject == g))
//            {
//                GetComponent<Rigidbody2D>().gravityScale = 10;
//            }
        }
        private void OnCollisionStay2D( Collision2D collision )
        {
            if (collision.gameObject.tag == "map")
            {
//                GetComponent<Rigidbody2D>().gravityScale = 0;
            }
        }
        float killtimer=0;
        public void Kill()
        {
            HP.current -= hit;
            GetComponent<ConstantForce2D>().force = new Vector2(0, 0);
            GetComponent<Rigidbody2D>().AddForce(new Vector2(10,0)) ;
            StartCoroutine(Killcon());
        }
        IEnumerator Killcon()
        {
            yield return new WaitForSeconds(0.01f);
            if (killtimer < 0.5f)
            {
                transform.position += new Vector3(0, 1, 0);
                transform.localScale -= new Vector3(0.02f, 0.02f, 0);
                StartCoroutine(Killcon());
                killtimer += 0.01f;
            }
            else
            {
                Destroy(gameObject);
            }
        }
        private void Update()
        {
            //transform.localPosition -= new Vector3(10,0,0);
        }
    }
}